package com.begye.smartloan.apptest.detailadsource

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.addBold
import com.begye.smartloan.apptest.ext.addNormal
import com.begye.smartloan.apptest.ext.addFontSize
import com.begye.smartloan.apptest.ext.addTextColor
import org.json.JSONObject

class DetailAfterItemAdapter(var dataList: List<JSONObject>) :
    RecyclerView.Adapter<DetailAfterItemAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_detail, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val listItemDetailTitle: TextView =
            view.findViewById(R.id.listItemDetailTitle)

        private val listItemDetailDetail: TextView =
            view.findViewById(R.id.listItemDetailDetail)


        fun bindData(position: Int, item: JSONObject) {

            val title = item.optString("title", "")
            listItemDetailTitle.text = item.optString("showName")
            listItemDetailDetail.text = item.optString("showDetail")

            if (title == "YES") {
                listItemDetailTitle.addTextColor("#000000")
                listItemDetailDetail.addTextColor("#000000")

                listItemDetailTitle.addBold()
                listItemDetailDetail.addBold()

                listItemDetailDetail.addFontSize(14F)

            } else {
                listItemDetailTitle.addTextColor("#999999")
                listItemDetailDetail.addTextColor("#999999")

                listItemDetailTitle.addNormal()
                listItemDetailDetail.addNormal()

                listItemDetailDetail.addFontSize(12F)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}